package com.example.jl.bethinner.domain;

import android.bluetooth.BluetoothDevice;

public class Device {
	String name;//设备名
    Long id;//设备id
    double temperature;//温度
    String Address;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getAddress() {
		return Address;
	}
	public void setAddress(String address) {
		Address = address;
	}
	public double getTemperature() {
		return temperature;
	}
	public void setTemperature(double temperature) {
		this.temperature = temperature;
	}
	
	
	public static Device fromScanData(BluetoothDevice device, int rssi,byte[] scanData) {
		int startByte = 3;//开始位置
		Device dev = new Device();
		dev.setAddress(device.getAddress());
		try {
			int nameLen = (int)scanData[startByte]-1;//设备name长度
			if(nameLen>0){
				byte[] name = new byte[nameLen];
				System.arraycopy(scanData, startByte + 2, name, 0, nameLen);
				dev.setName(new String(name,"ascii"));
				startByte+=2+nameLen;
			}else {
				if(nameLen==0)
					dev.setName("");
				else
					return null;
			}
			if(scanData[startByte+2]== 'I' &&
			   scanData[startByte+3]== 'O' &&
			   scanData[startByte+4]== 'T' ){
				startByte+=5;
				byte[] temp = new byte[4];
				System.arraycopy(scanData, startByte, temp, 0, 4);//获取设备id，将16进制转成10进制
                dev.setId(Long.parseLong(new String(temp, "ascii"), 16));
                System.arraycopy(scanData, startByte + 4, temp, 0, 4);
                dev.setTemperature(Long.parseLong(new String(temp, "ascii"), 16)/100.0);//获取设备温度，将16进制转成10进制并/100
			}else {
				return null;
			}
			if(!(scanData[startByte+9]=='E'&&
			   scanData[startByte+10]=='N'&&
			   scanData[startByte+11]=='D')){
				return null;
			}
		} catch (Exception e) {
			return null;
		}
		return dev;
	}
}
